Concept:
Algebraic data type
Headline
A type for the construction of terms
Illustration
Algebraic data types are typically supported by functional programming languages. For instance, Language:Haskell, Language:Scala, and Language:SML support algebraic data types. Illustrations are given here for Language:Haskell.
A data type for shapes can be defined as follows:
data Shape = Circle Float
| Rectangle Float Float
The data contructor Circle serves for the representation of circles; the one and only constructor component serves for the radius. The data constructor Rectangle serves for the representation of rectangles; the two constructor components serve for width and height.
Constructors can be used as functions to construct terms:
myCircle :: Shape
myCircle = Circle 42
myRectangle :: Shape
myRectangle = Rectangle 77 88
In fact, constructors are functions with the types of the constructor components as argument types and the type of algebraic data type as the result type:
> :t Circle
Circle :: Float -> Shape
> :t Rectangle
Rectangle :: Float -> Float -> Shape
Pattern matching can be applied to terms of algebraic data types:
-- Test whether the shape is a circle
isCircle (Circle _) = True
isCircle (Rectangle _ _) = False
See Contribution:haskellData for a more profound illustration of algebraic data types.
All predefined, compound types of Haskell are essentially algebraic data types. For instance, Haskell's Booleans could be defined by an algebraic data type with two data constructors as follows:
data Bool = True | False
Haskell's lists could be defined by an algebraic data type with two construtors as follows:
data List a = Nil | Cons a (List a)
The constructors Nil and Cons are meant to correspond to the empty list and ":" of Haskell's built-in lists. The Nil constructor has no constructor component, whereas the Cons constructor has two constructor components.
See also Maybe types for yet another illustration of algebraic data types.
Backlinks
User contributions
User edits
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.